 import moment from "moment";

// 格式化时间
export function getTime(val, format) {
    let f = format || "YYYY-MM-DD HH:mm:ss";
    return moment(val).format(f);
}

// 查找父级
export function getParent(list, childId) {
    let arrRes = [];
    if (list.length == 0) {
        if (childId) {
            arrRes.unshift(list);
        }
        return arrRes;
    }
    let rev = (data, nodeId) => {
        for (let i = 0, length = data.length; i < length; i++) {
            let node = data[i];
            if (node.id == nodeId) {
                arrRes.unshift(node);
                rev(list, node.parentId);
                break;
            } else {
                if (node.children) {
                    rev(node.children, nodeId);
                }
            }
        }
        return arrRes;
    };
    arrRes = rev(list, childId);
    return arrRes;
}

// 反转树
export function invertTree(data) {
    let res = []
    let arr = JSON.parse(JSON.stringify(data))
    let findChildName = (list) => {
        list.forEach(item => {
            if (item.children) {
                findChildName(item.children)
                delete item.children
                res.push(item)
            } else {
                res.push(item)
            }
        });
    }
    findChildName(arr)
    return res
}

// 重构数组 (转树)
export function refactoringArr(list, option, parId, disableCheckbox, scopedSlots) {

    function loop(parId) {
        let definedItem = {}
        const res = []
        for (let i = 0; i < list.length; i++) {
            const baseItem = list[i]

            //树添加eCheckbox置灰
            if (disableCheckbox) {
                baseItem["disableCheckbox"] =
                    baseItem["resourceState"] == 1 ? false : true;
            }
            //树标题自定义
            if (scopedSlots) baseItem["scopedSlots"] = { title: 'custom' }
            if (option) definedItem = { ...option(baseItem) }
            if (baseItem['parentId'] === parId) {
                baseItem['children'] = loop(baseItem['id'])
                if (option) definedItem['children'] = loop(baseItem['id'])
                if (baseItem['children'].length === 0) {
                    delete baseItem['children']
                    if (option) delete definedItem['children']
                }
                res.push(option ? definedItem : baseItem)
            }
        }
        return res
    }
    return loop(parId)
}

// 查找子级所有id
export function findChildIdArr(list, id) {
    const res = []
    for (let i = 0; i < list.length; i++) {
        const baseItem = list[i]
        if (baseItem.children) {
            findChild(baseItem.children, id, 'match')
        }
        matchFun(baseItem, id)
    }
    function matchFun(matchItem, id) {
        if (matchItem['id'] === id) {
            res.push(matchItem['id'])
            if (matchItem.children) {
                findChild(matchItem.children, id)
            }
        }
    }
    function findChild(item, id, match) {
        for (let y = 0; y < item.length; y++) {
            if (match) {
                matchFun(item[y], id)
                if (item[y].children) {
                    findChild(item[y].children, id, 'match')
                }
            }
            else {
                res.push(item[y].id)
                if (item[y].children) {
                    findChild(item[y].children, id)
                }
            }
        }
    }
    return res
}

//对象传入转换formdata
export function objFormData(datas) {
    console.log(datas)
    const fd = new FormData();
    if (Array.isArray(datas)) {
        datas.forEach(file => {
           if(file) fd.append("file", file);
        });
    } else {
        Object.keys(datas).forEach(key => {
            if(datas[key]) fd.append(key, datas[key]);
        });
    }
    return fd;
}

// Array to string
export function arrayToString(arr, val) {
    const vals = [];
    arr.forEach(ele => {
      vals.push(ele[val]);
    });
    return vals.join(',');
  }

  //获取id列表
  export function getIds(arr, id) {
    let ids = [];
    if (arr && arr.length > 0) {
      arr.forEach(item => {
        ids.push(item[id]);
      });
    }
    return ids;
  }

  
  
// 树状图添加对应的disabled
export function addTreeAddDisabled(list, id, disabled, status) {
    const res = [];
    for (let i = 0; i < list.length; i++) {
      const baseItem = list[i];
      baseItem[disabled] = false;
      res.push(baseItem);
      if (baseItem.children) {
        findChild(baseItem.children);
      }
      if (id && baseItem["id"] == id) {
        baseItem[disabled] = status;
        if (baseItem.children) {
          findChild(baseItem.children, "match");
        }
      }
    }
    function findChild(item, match) {
      for (let y = 0; y < item.length; y++) {
        item[y][disabled] = false;
        if (match || (id && item[y]["id"] == id)) {
          item[y][disabled] = status;
          if (item[y].children) {
            findChild(item[y].children, "match");
          }
        } else {
          if (item[y].children) {
            findChild(item[y].children);
          }
        }
        res.push(item);
      }
    }
    return list;
}

// 处理词典标签
export function dealDic(dicArr, auditState, property){
    if(!auditState) auditState = 0
    let res = dicArr.find((i)=>{return i.value== auditState})
    return res[property]
}